// components/site/button/button.js
import create from "../../../westore/utils/create"
import Api from "../../../utils/api"
import deepCopy from "../../../utils/common.js"
const regeneratorRuntime = require("../../../utils/regenerator-runtime/runtime")
create({
  properties: {
    act_id: {
      type: String,
      value: ""
    },
    isSelf:{
      type:Boolean,
      value: false
    },
    theopenid:{
      type: String,
      value:""
    },
    check:{
      type: Boolean,
      value: true,
    },
    likenum:{
      type: String,
      value: "0"
    }
  },
  data: {
    commentData: [],//评论数据
    likeData:[],
    commentVar: {},
    formUrl: '/pages/index/index',
    openid:""
  },

  attached() {
    // 在组件实例进入页面节点树时执行
    this.getCommentdata();
  },
  detached() {
    // 在组件实例被从页面节点树移除时执行
  },
  
  /**
   * 获取评论数据
   */
  methods:{
    async getCommentdata() {
      const _this = this;
      const res = await Api.getReply(_this.data.act_id);
      if (res.status == 0) {
        _this.setData({
          commentData: res.data,
        })
        return true;
      } else {
        wx.showModal({
          title: '提示',
          content: '获取活动数据失败，请检查网络',
          showCancel: false
        })
        return false;
      }
    },
    /**
   * 点击评论
   */
    clickReply(e) {
      const _this = this;
      const dataset = e.currentTarget.dataset
      const actid= _this.data.act_id;
      const theid= dataset.theid;
      const opid= dataset.opid;
      const myopenid = _this.store.data.openid;
      if (opid == myopenid) {
        wx.showActionSheet({
          itemList: ['删除'],
          success(res) {
            // _this.delComment(actid, myopenid, theid)
            _this.delComment(myopenid, theid)
          },
          fail(res) {
            console.log(res.errMsg)
          }
        })
        return
      }
      //判断是否是本人的活动
      // if (myopenid != theopenid) return

      // //通过reply标志判断是否可评论
      // if (reply == true) return

      // this.setData({
      //   releaseFocus: true,
      //   ["commentVar.act_id"]: actid,
      //   ["commentVar.openid"]: myopenid,
      //   ["commentVar.talkeropenid"]: opid,
      //   ["commentVar.commentid"]: theid,
      // })
    },
    // quxiao
    subCancel() {
      this.setData({
        releaseFocus: false,
      })
    },
    // 设置值
    setTalkData(e) {
      this.setData({
        ["commentVar.talk"]: e.detail.value
      })
    },
    /**
     * 发送评论
     */
    async submit() {
      const _this = this
      const data = _this.data.commentVar;
      const openid = _this.store.data.openid;
      const actid = _this.data.act_id;
      if (data.talk == '') {
        wx.showModal({
          title: '提示',
          content: '请输入内容',
          showCancel: false,
        })
        return
      } else {
        wx.showLoading({
          title: '请稍后...',
        })
        const res = await Api.postReply(openid,actid,data.talk)
        if (res.status == 0) {
          wx.showToast({
            title: '评论成功',
          })
          await _this.getCommentdata();
        } else if (res.status == 1) {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '评论失败，请检查网络',
            showCancel: false
          })
        }
        wx.hideLoading();
        _this.setData({
          releaseFocus: false,
          talk: ''
        })
      }
    },
    /**
     * 删除评论
     */
    async delComment(openid, theid) {
      wx.showLoading({
        title: '处理中...',
      })
      console.log('删除评论')
      const data = this.data
      const _this = this
      const res = await Api.deleteReply(openid,theid)
      console.log(res)
      wx.hideLoading();
      if (res.status == 0) {
        _this.getCommentdata();
        wx.showToast({
          title: '删除成功', //标题
          icon: 'success', //图标，支持"success"、"loading"
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '删除失败，请检查网络',
          showCancel: false
        })
      }
    },
    // 删除活动
    delActivity() {
      const _this = this
      const actid = _this.data.act_id
      const openid = _this.store.data.openid;
      wx.showModal({
        title: '提示',
        content: '确认删除动态？',
        success: async function (res) {
          if (res.cancel) {
            //点击取消,默认隐藏弹框
          } else {
            //点击确定
            wx.showLoading({
              title: '处理中...',
            })
            const delres = await Api.deleteActivity(openid,actid);
            wx.hideLoading();
            // console.log(res)
            if (delres.status == 0) {
              _this.triggerEvent('uploadData', {}, { bubbles: true })
              wx.showToast({
                title: '删除成功', //标题
                icon: 'success', //图标，支持"success"、"loading"
              })
              wx.navigateBack();
              return;
            } else {
              wx.showModal({
                title: '提示',
                content: '删除动态失败，请检查网络',
                showCancel: false
              })
            }
          }
        }
      })
    },
    // 点赞
    async cilckLike(e) {
      wx.showLoading({
        title: '处理中...',
      })
      const _this = this;
      const actid = _this.data.act_id;
      const openid = _this.store.data.openid
      const res = await Api.postLike(openid,actid)
      if (res.status == 0) {
        _this.setData({
          liked:!_this.data.liked,
          likenum:res.data.like
        })
      wx.hideLoading();
      } else {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '点赞失败，请检查网络',
          showCancel: false
        })
      }
    },
    goComment: function (e) {
      const _this = this
      _this.setData({
        releaseFocus: true
      })
    },
  }
})